
#pragma once

#include "type.h"

class FrameAnimation : public CCSprite {
public:
	FrameAnimation(void);
	virtual ~FrameAnimation(void);
    
private:
	float _frameRate;
	float _frameRateFactor;
    
	int _frameIndex ;
	bool _isLoop ;
	bool _paused ;
	bool _finished;
    bool _needRemoveAfterFinished;
    
	int _frameCount;
	float _elapsedTime;
    
    vector<string> _frames;
    
    string _text;
    string _animKey;
    CCPoint _logicPosition;
public:
    void setLogicPosition(const CCPoint& pos);

    CCPoint getLogicPosition() {
        return _logicPosition;
    }
	void set(const AnimationInfo& info);
	void setFrame(int index);
    void update(float ticks);
    
    inline void setText(const char* str){
        _text = str;
    }
    
    inline const char* getText(){
        return _text.c_str();
    }
    
    const string& getAnimKey() {
        return _animKey;
    }
    
	inline void restart (){
		_frameIndex = 0;
		_finished = false;
		_paused = false;
        
        setFrame(0);
	}
    
	inline bool isPaused(){
		return _paused;
	};
    
	inline void pause(){
		_paused = true;
	};
    
	inline void resume(){
		_paused = false;
		_finished = false;
	}
    
	inline bool isFinished(){
		return _finished;
	}
    
    inline void setFinished(bool isFinished){
        _finished = isFinished;
    }
    
	inline void setLoop(bool useLoop){
		_isLoop = useLoop;
	}

	inline bool isLoop() {
		return _isLoop;
	}
    
    inline bool  isNeedRemoveAfterFinished() {
        return _needRemoveAfterFinished;
    }
    
    inline void setNeedRemoveAfterFinished(bool needRemove){
        _needRemoveAfterFinished = needRemove;
    }

	inline int getFrameCount() {
		return _frameCount;
	}

	inline float getFrameRate() {
		return _frameRate;
	}

	inline int getCurrentFrameIndex() {
		return _frameIndex;
	}

	inline void setFrameRateFactor(float num) {
		_frameRateFactor = 1 / num ;
	}
};

class AnimationSequence {
public:
	AnimationSequence(int id);

	~AnimationSequence();

	bool isFinished();

	void playFirst();

	void playNext();

	void update(float ticks);

	void add(FrameAnimation * anim, bool needLoop, float _arrDurations);

	CCNode* getParent() {
		return _parent;
	}

	void setParent(CCNode* p) {
		_parent = p;
	}

	int getCount() {
		return _arrAnims.size();
	}

	int getID() {
		return _id;
	}

private:
	int _id;
	int _playingAnimIndex;
	
	vector<FrameAnimation *> _arrAnims;
	vector<bool> _arrLoops;
	vector<float> _arrDurationss;

	float _timeElapsed;

	CCNode* _parent;
};
